﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Application.Admin.Dto;

namespace Presentation.Web.Admin.Areas.Open.Controllers
{
    public class SightController : Infrastructure.Crosscutting.NetFramework.Controllers.OpenBaseController
    {
        Application.Admin.Sight.ISightPointAreaService sightAreaService;

        Application.Admin.Sight.ISightPointClassService sightClassService;

        Application.Admin.Sight.ISightPointService sightService;

        Application.Admin.Sight.ISightPointGoodsService sightGoodsService;

        Application.Admin.Sight.ISightPointCommReplyService sightReplyService;

        Application.Admin.Sight.ISightPointCommService sightCommService;

        Application.Admin.Sight.ISightPointUserSiginService sightSiginService;

        Application.Admin.MobileClient.IMobileClientService clientService;

        Application.Admin.Goods.IGoodsService goodsService;

        Application.Admin.Sight.IOpenSightService openSightService;

        Infrastructure.Crosscutting.Authorize.IUserService userService;

        Domain.Uow.IRepository<Domain.Entity.SightPointVersion> sightVersionRepository;

        public SightController(Application.Admin.Sight.ISightPointAreaService sightArea,
            Application.Admin.Sight.ISightPointClassService sightClass,
            Application.Admin.Sight.ISightPointService sight,
            Application.Admin.Sight.ISightPointGoodsService sightGoods,
            Application.Admin.Sight.ISightPointCommReplyService reply,
            Application.Admin.Sight.ISightPointCommService comm,
            Application.Admin.Sight.ISightPointUserSiginService sigin,
            Application.Admin.MobileClient.IMobileClientService client,
            Application.Admin.Sight.IOpenSightService openSight,
            Infrastructure.Crosscutting.Authorize.IUserService user,
            Domain.Uow.IRepository<Domain.Entity.SightPointVersion> sightVersion)
        {
            if (sightArea == null)
            {
                throw new ArgumentNullException("sightAreaService is null");
            }
            if (sightClass == null)
            {
                throw new ArgumentNullException("sightClassService is null");
            }
            if (sight == null)
            {
                throw new ArgumentNullException("sightService is null");
            }
            if (sightGoods == null)
            {
                throw new ArgumentNullException("sightGoodsService is null");
            }
            if (reply == null)
            {
                throw new ArgumentNullException("replyService is null");
            }
            if (comm == null)
            {
                throw new ArgumentNullException("commService is null");
            }
            if (sigin == null)
            {
                throw new ArgumentNullException("siginService is null");
            }
            if (client == null)
            {
                throw new ArgumentNullException("clientService is null");
            }
            if (openSight == null)
            {
                throw new ArgumentNullException("openSightService is null");
            }
            if (user == null)
            {
                throw new ArgumentNullException("userService is null");
            }
            if (sightVersion == null)
            {
                throw new ArgumentNullException("sightVersionRepository is null");
            }
            sightAreaService = sightArea;
            sightClassService = sightClass;
            sightService = sight;
            sightGoodsService = sightGoods;
            sightReplyService = reply;
            sightCommService = comm;
            sightSiginService = sigin;
            clientService = client;
            openSightService = openSight;
            userService = user;
            sightVersionRepository = sightVersion;
        }

        #region comm

        public JsonResult GetSightCommCountBySightInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            var count = 0;
            if (entity != null)
            {
                count = sightCommService.GetList().Where(e => e.SightId == entity.Id && e.State == true).Count();
            }
            return Json(count, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightCommCountBySightLatLon(double lat, double lon)
        {
            var entity = sightService.GetList().Where(e => e.Lat == lat && e.Lon == lon).FirstOrDefault();
            var count = 0;
            if (entity != null)
            {
                count = sightCommService.GetList().Where(e => e.SightId == entity.Id && e.State == true).Count();
            }
            return Json(count, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightCommCountBySightName(string name)
        {
            var count = 0;
            count = sightCommService.GetList()
                .Where(e => e.SightName == name && e.State == true).Count();
            return Json(count, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightCommByCommId(int id)
        {
            var data = sightCommService.GetList().Where(e => e.Id == id && e.State == true)
                .OrderByDescending(e => e.Id).AsQueryable();
            var temp = openSightService.GetSightOpenComm(data).FirstOrDefault();
            if (temp == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(temp, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightCommList(int pi, int take)
        {
            var data = sightCommService.GetList().Where(e => e.State == true)
                .OrderByDescending(e => e.Id).AsQueryable();
            var total = data.Count();
            take = take > 10 ? 10 : take;
            data = data.Skip(((pi - 1) > 0 ? (pi - 1) : 0) * take).Take(take);
            var temp = openSightService.GetSightOpenComm(data);
            return Json(new { total = total, data = temp }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightCommListBySightInternalId(int id, int pi, int take)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = sightCommService.GetList().Where(e => e.SightId == entity.Id && e.State == true)
                    .OrderByDescending(e => e.Id).AsQueryable();
                var total = data.Count();
                take = take > 10 ? 10 : take;
                data = data.Skip(((pi - 1) > 0 ? (pi - 1) : 0) * take).Take(take);
                var temp = openSightService.GetSightOpenComm(data);
                return Json(new { total = total, data = temp }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightCommListBySightName(string name, int pi, int take)
        {
            var data = sightCommService.GetList().Where(e => e.SightName.Contains(name) && e.State == true)
                .OrderByDescending(e => e.Id).AsQueryable();
            var total = data.Count();
            take = take > 10 ? 10 : take;
            data = data.Skip(((pi - 1) > 0 ? (pi - 1) : 0) * take).Take(take);
            var temp = openSightService.GetSightOpenComm(data);
            return Json(new { total = total, data = temp }, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult AddSightComm(OpenParaAddSightCommDto dto)
        {
            var res = false;
            var message = "";
            if ((!string.IsNullOrEmpty(dto.SightName) || dto.SightInternalId > 0) && !string.IsNullOrEmpty(dto.Content))
            {
                if (string.Compare(PreAdminConfigSettingService
                .IsCheckOpenUser, "1", false) == 0)
                {
                    if (!string.IsNullOrEmpty(dto.OperName) && !string.IsNullOrEmpty(dto.OperPwd))
                    {
                        var operUser = userService.GetUserModelByUserNameAndPwd(dto.OperName, dto.OperPwd);
                        if (operUser == null)
                        {
                            return Json("", JsonRequestBehavior.AllowGet);
                        }
                    }
                    else
                    {
                        return Json("", JsonRequestBehavior.AllowGet);
                    }
                }

                Domain.Entity.SightPoint sight = null;
                if (dto.SightInternalId > 0)
                {
                    sight = sightService.GetList().Where(e => e.InternalId == dto.SightInternalId).FirstOrDefault();
                }
                else if (!string.IsNullOrEmpty(dto.SightName))
                {
                    sight = sightService.GetList().Where(e => e.Name == dto.SightName).FirstOrDefault();
                }
                if (sight != null && sight.Id > 0)
                {
                    var comm = new Domain.Entity.SightPointComm();
                    comm.AddTime = System.DateTime.Now;
                    var client = clientService.GetList().Where(e => e.Name == dto.ClientType).FirstOrDefault();
                    if (client != null)
                    {
                        comm.ClientId = client.Id;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(dto.ClientType))
                        {
                            client = new Domain.Entity.MobileClient();
                            client.AddTime = System.DateTime.Now;
                            client.IpAddress = "127.0.0.1";
                            client.Name = dto.ClientType;
                            client.Remark = dto.ClientType;
                            client.SortNum = 0;
                            client.State = true;
                            clientService.Add(client, userService.GetDefautlOperUser());
                            comm.ClientId = client.Id;
                        }
                    }
                    comm.Contents = dto.Content;
                    comm.IpAddress = "127.0.0.1";
                    comm.SightId = sight.Id;
                    comm.SightName = sight.Name;
                    comm.SortNum = 0;
                    comm.State = true;
                    comm.IsReply = false;
                    var user = userService.GetUserModelByName(dto.UserName);
                    if (user != null && user.Id > 0)
                    {
                        comm.UserId = user.Id;
                        comm.UserName = string.IsNullOrEmpty(user.UserName) == true ? "游客" : user.UserName;
                    }
                    else
                    {
                        comm.UserName = string.IsNullOrEmpty(dto.UserName) == true ? "游客" : dto.UserName;
                    }
                    try
                    {
                        sightCommService.Add(comm, user);
                        if (comm.Id > 0)
                        {
                            res = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        message = ex.Message;
                    }
                }
            }
            return Json(new { success = res, message = message }, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region singin

        public JsonResult GetSightSiginCountBySightInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            var count = -1;
            if (entity != null)
            {
                count = sightSiginService.GetList().Where(e => e.SightId == entity.Id).Count();
            }
            return Json(count, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightSiginCountBySightName(string name)
        {
            var data = sightSiginService.GetList().Where(e => e.SightName == name).Count();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult AddSightSigin(OpenParaAddSightSiginDto dto)
        {
            var count = -1;
            if (string.Compare(PreAdminConfigSettingService
                .IsCheckOpenUser, "1", false) == 0)
            {
                if (!string.IsNullOrEmpty(dto.OperName) && !string.IsNullOrEmpty(dto.OperPwd))
                {
                    var operUser = userService.GetUserModelByUserNameAndPwd(dto.OperName, dto.OperPwd);
                    if (operUser == null)
                    {
                        return Json("", JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
            }

            if (dto.SightInternalId > 0)
            {
                var entity = sightService.GetList().Where(e => e.InternalId == dto.SightInternalId).FirstOrDefault();
                if (entity != null && entity.Id > 0)
                {
                    #region internal id add

                    var sigin = new Domain.Entity.SightPointUserSigin();
                    sigin.AddTime = System.DateTime.Now;
                    var client = clientService.GetList().Where(e => e.Name == dto.ClientType).FirstOrDefault();
                    if (client != null)
                    {
                        sigin.ClientId = client.Id;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(dto.ClientType))
                        {
                            client = new Domain.Entity.MobileClient();
                            client.AddTime = System.DateTime.Now;
                            client.IpAddress = "127.0.0.1";
                            client.Name = dto.ClientType;
                            client.Remark = dto.ClientType;
                            client.SortNum = 0;
                            client.State = true;
                            clientService.Add(client, userService.GetDefautlOperUser());
                            sigin.ClientId = client.Id;
                        }
                    }
                    sigin.Contents = dto.Content;
                    sigin.IpAddress = "127.0.0.1";
                    sigin.SightId = entity.Id;
                    sigin.SightName = entity.Name;
                    sigin.SortNum = 0;
                    sigin.State = true;
                    var user = userService.GetUserModelByName(dto.UserName);
                    if (user != null && user.Id > 0)
                    {
                        sigin.UserId = user.Id;
                        sigin.UserName = string.IsNullOrEmpty(user.UserName) ? "游客" : user.UserName;
                    }
                    else
                    {
                        sigin.UserName = string.IsNullOrEmpty(dto.UserName) ? "游客" : dto.UserName;
                    }
                    try
                    {
                        sightSiginService.Add(sigin, user);
                        if (sigin.Id > 0)
                        {
                            count = sightSiginService.GetList().Where(e => e.SightId == entity.Id).Count();
                        }
                    }
                    catch (Exception ex)
                    {
                    }

                    #endregion
                }
            }
            else if (!string.IsNullOrEmpty(dto.SightName))
            {
                var sight = sightService.GetList().Where(e => e.Name == dto.SightName).FirstOrDefault();
                if (sight != null && sight.Id > 0)
                {
                    #region name add


                    var sigin = new Domain.Entity.SightPointUserSigin();
                    sigin.AddTime = System.DateTime.Now;
                    var client = clientService.GetList().Where(e => e.Name == dto.ClientType).FirstOrDefault();
                    if (client != null)
                    {
                        sigin.ClientId = client.Id;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(dto.ClientType))
                        {
                            client = new Domain.Entity.MobileClient();
                            client.AddTime = System.DateTime.Now;
                            client.IpAddress = "127.0.0.1";
                            client.Name = dto.ClientType;
                            client.Remark = dto.ClientType;
                            client.SortNum = 0;
                            client.State = true;
                            clientService.Add(client, userService.GetDefautlOperUser());
                            sigin.ClientId = client.Id;
                        }
                    }
                    sigin.Contents = dto.Content;
                    sigin.IpAddress = "127.0.0.1";
                    sigin.SightId = sight.Id;
                    sigin.SightName = sight.Name;
                    sigin.SortNum = 0;
                    sigin.State = true;
                    var user = userService.GetUserModelByName(dto.UserName);
                    if (user != null && user.Id > 0)
                    {
                        sigin.UserId = user.Id;
                        sigin.UserName = user.UserName;
                    }
                    else
                    {
                        sigin.UserName = dto.UserName;
                    }
                    try
                    {
                        sightSiginService.Add(sigin, user);
                        if (sigin.Id > 0)
                        {
                            count = sightSiginService.GetList().Where(e => e.SightId == sight.Id).Count();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    #endregion
                }
            }
            return Json(count, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region sight

        public JsonResult GetAllSight()
        {
            var res = openSightService.GetAllOpenSightNoPic();
            if (res != null && res.Any())
            {
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetAllSightAllOpenPic()
        {
            var res = openSightService.GetAllSightAllOpenPic();
            if (res.Count > 0)
            {
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetAllSightWithOnePic()
        {
            var res = openSightService.GetAllOpenSightWithOneOpenPic();
            if (res != null && res.Any())
            {
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightVersion()
        {
            var res = 0;
            var version = sightVersionRepository.GetList().FirstOrDefault();
            if (version != null)
            {
                res = version.Version;
            }
            return Json(res, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightByInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetOpenSightNoPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightByName(string name)
        {
            var entity = sightService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetOpenSightNoPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightWithOnePicByInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetOpenSightWithOneOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightWithOnePicByName(string name)
        {
            var entity = sightService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetOpenSightWithOneOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }


        #region added by yjihrp 2012.5.11.9.11  没有暴露出去的接口

        public JsonResult GetSightOpenPicListBySightInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightOpenPicList(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(new { total = data.Count, data = data }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightOpenPicListBySightName(string name)
        {
            var entity = sightService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightOpenPicList(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(new { total = data.Count, data = data }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightRandOpenPicBySightInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightRandOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightRandOpenPicBySightName(string name)
        {
            var entity = sightService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightRandOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightLatestOpenPicBySightInternalId(int id)
        {
            var entity = sightService.GetList().Where(e => e.InternalId == id).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightLatestOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetSightLatestOpenPicBySightName(string name)
        {
            var entity = sightService.GetList().Where(e => e.Name == name).FirstOrDefault();
            if (entity != null)
            {
                var data = openSightService.GetSightLatestOpenPic(entity);
                if (data == null)
                {
                    return Json("", JsonRequestBehavior.AllowGet);
                }
                return Json(data, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
        }

        #endregion

        #endregion

        #region Area

        public JsonResult GetSightAreaByName(string name)
        {
            var data = sightAreaService.GetList()
                .Where(e => e.Name == name && e.State == true)
                .OrderByDescending(e => e.Id).FirstOrDefault();
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(data.ToDto(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightAreaById(int id)
        {
            var data = sightAreaService.GetList()
                .Where(e => e.Id == id && e.State == true)
                .OrderByDescending(e => e.Id).FirstOrDefault();
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(data.ToDto(), JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region class

        public JsonResult GetSightClassByName(string name)
        {
            var data = sightClassService.GetList().Where(e => e.Name == name)
                .OrderByDescending(e => e.Id).FirstOrDefault();
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(data.ToDto(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetSightClassById(int id)
        {
            var data = sightClassService.GetList().Where(e => e.Id == id && e.State == true)
                .OrderByDescending(e => e.Id).FirstOrDefault();
            if (data == null)
            {
                return Json("", JsonRequestBehavior.AllowGet);
            }
            return Json(data.ToDto(), JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
